
package com.zjxd.business.web.api.order;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjxd.business.web.common.ApiResult;
import com.zjxd.business.web.common.BaseService;
import com.zjxd.business.web.model.dto.OrderDto;
import com.zjxd.business.web.model.pojo.TBUser;
import com.zjxd.business.web.model.vo.OrderVo;
import com.zjxd.business.web.utils.TimeUtil;

@Service
public class TBOrderServiceImpl extends BaseService implements TBOrderService {

	public ApiResult listPage(OrderModel model) {

		OrderDto dto = new OrderDto();
		dto.setPhone(model.getPhone());
		dto.setStartTime(model.getStartTime());
		dto.setEndTime(model.getEndTime());
		dto.setOrderstate(model.getOrderstate());
		PageHelper.startPage(model.getPageNum(), model.getPageSize());
		List<OrderVo> tBOrderList = tBOrderMapper.selectOrder(dto);
		PageInfo<OrderVo> pageInfo = new PageInfo<>(tBOrderList);
		return ApiResult.success(pageInfo);
	}

	/**
	 * 批量导出订单信息
	 */
	@SuppressWarnings("resource")
	public int export(String ids, String orderstate,HttpServletResponse response) {

		// 将字符串转换成整型数组
		String[] id = ids.split(",");
		List<Long> idList = new ArrayList<Long>();
		for (int i = 0; i < id.length; i++) {
			idList.add(Long.parseLong(id[i]));
		}
		OrderDto dto = new OrderDto();
		dto.setOrderids(idList);
		dto.setOrderstate(Integer.parseInt(orderstate));
		List<OrderVo> varlogList = tBOrderMapper.selectOrder(dto);
		// 创建workbook
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 创建sheet页
		HSSFSheet sheet = workbook.createSheet("充值记录列表");

		// 创建单元格
		HSSFRow row = sheet.createRow(0);
		HSSFCell c0 = row.createCell(0);
		c0.setCellValue(new HSSFRichTextString("订单编号"));

		HSSFCell c1 = row.createCell(1);
		c1.setCellValue(new HSSFRichTextString("充电桩编号"));

		HSSFCell c2 = row.createCell(2);
		c2.setCellValue(new HSSFRichTextString("用户账号"));

		HSSFCell c3 = row.createCell(3);
		c3.setCellValue(new HSSFRichTextString("订单开始时间"));

		HSSFCell c4 = row.createCell(4);
		c4.setCellValue(new HSSFRichTextString("充电时长(分钟)"));

		HSSFCell c5 = row.createCell(5);
		c5.setCellValue(new HSSFRichTextString("预付金额(元)"));

		int num = 1;
		for (int i = 0; i < varlogList.size(); i++) {

			// 获得充值记录
			OrderVo vo = varlogList.get(i);
			if (vo == null) {
				continue;
			}
			// 创建行对象
			HSSFRow rownum = sheet.createRow(num);

			HSSFCell cnum0 = rownum.createCell(0);
			cnum0.setCellValue(new HSSFRichTextString(vo.getOrdernum()));

			HSSFCell cnum1 = rownum.createCell(1);
			cnum1.setCellValue(new HSSFRichTextString(vo.getPilenum()));

			HSSFCell cnum2 = rownum.createCell(2);
			cnum2.setCellValue(new HSSFRichTextString(vo.getUserphone()));

			HSSFCell cnum3 = rownum.createCell(3);
			if (vo.getStarttime() != null && !"".trim().equals(vo.getStarttime())) {
				cnum3.setCellValue(new HSSFRichTextString(TimeUtil.format(vo.getStarttime(), TimeUtil.FORMAT_LONG)));
			} else {
				cnum3.setCellValue(new HSSFRichTextString("-"));
			}

			HSSFCell cnum4 = rownum.createCell(4);
			if (vo.getEndtime() != null && !"".trim().equals(vo.getEndtime())) {
				cnum4.setCellValue(new HSSFRichTextString(vo.getEndtime().toString()));
			} else {
				cnum4.setCellValue(new HSSFRichTextString("-"));
			}

			HSSFCell cnum5 = rownum.createCell(5);
			if (vo.getPrice() != null && !"".trim().equals(vo.getPrice())) {
				cnum5.setCellValue(new HSSFRichTextString(vo.getPrice().toString()));
			} else {
				cnum5.setCellValue(new HSSFRichTextString("-"));
			}
			num++;
		}

		// 第六步，将文件存到指定位置
		try {
			Calendar cal = Calendar.getInstance();
			String fileName = "用户信息" + cal.get(Calendar.YEAR) + "年" + (cal.get(Calendar.MONTH) + 1) + "月" + cal.get(Calendar.DATE) + "日" + cal.get(Calendar.HOUR_OF_DAY) + "时" + cal.get(Calendar.MINUTE) + "分" + cal.get(Calendar.SECOND) + "秒" + ".xls";;
			fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);// 指定下载的文件名
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			OutputStream output = response.getOutputStream();
			workbook.write(output);
			output.flush();
			output.close();
		} catch (Exception e) {
			return 0;
		}
		return 1;
	}
}